package com.road.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.road.filter.CharSetFilter;
import com.road.model.Msg;
import com.road.model.User;
import com.road.service.IUserService;
import com.road.service.imp.UserServiceImp;
import com.road.utils.Base64Coder;
import com.road.utils.CodeUtil;
import com.road.utils.Consists;
import com.road.utils.ResponseUtil;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet(name = "register", description = "邮箱注册", urlPatterns = { "/register" })
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger log=Logger.getLogger(RegisterServlet.class.getName());
	IUserService userService=new UserServiceImp();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user_phone=request.getParameter("user_phone");
		String user_pass=request.getParameter("user_pass");
		String user_email=request.getParameter("user_email");
		List<Object> list =new ArrayList<>();
		list.add(verifyEmailRegester(user_phone, user_pass, user_email));
		ResponseUtil.sendScript(response, JSON.toJSONString(list));
		
	}
    //  邮件验证激活注册注册
	private Msg verifyEmailRegester(String user_phone, String user_pass, String user_email) {
		Msg msg=new Msg();
		User user=new User(user_phone, user_email, Base64Coder.encodeString(user_pass), 0, CodeUtil.generateUniqueCode()+CodeUtil.generateUniqueCode());
		try {
 		 //如果不需要邮件验证,直接进行注册，否则验证邮件格式是否正确，若格式正确验证邮件是否已经被使用，否则直接注册
			if(user_email!=null&&user_email.length()>0) {
				if (!userService.cheakEmail(user_email)) {
					msg.setCode(Consists.Error_Code_MailFormatError);
					msg.setMsg(Consists.Error_Msg__MailFormatError);
				}else {
					if(userService.cheakUserByEmail(user)) {
						msg.setCode(Consists.Error_Code_EmailIsUsing);
						msg.setMsg(Consists.Error_Msg_EmailIsUsing);
					}else {
						if (userService.getUserByPhone(user)) {
							msg.setCode(Consists.Error_Code_EmailIsUsing);
							msg.setMsg(Consists.Error_Msg_EmailIsUsing);
						}else {
							register(msg, user,true);
						}
						
					}
				}
			
			}else {
				if (userService.getUserByPhone(user)) {
					msg.setCode(Consists.Error_Code_RegisterOK);
					msg.setMsg(Consists.Error_Msg__COUNT_ISUSING);
				}else {
					register(msg, user,false);
				}
				
			}
		} catch (Exception e) {
			// TODO: handle exception
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		return msg;
	}
    //用户注册
	private void register(Msg msg, User user,boolean hasEmail) throws SQLException {
		//注册成功
		if(userService.doRegister(user)){
			msg.setCode(Consists.Error_Code_RegisterOK);
			if(hasEmail) {
				msg.setMsg(Consists.Error_Msg__RegisterOK_EMAIL);
			}else {
				msg.setMsg(Consists.Error_Msg__RegisterOK);
			}

		}else{
			msg.setCode(Consists.Error_Code_RegisterFail);
			msg.setMsg(Consists.Error_Msg__RegisterFail);

		}
	}
	
	

}
